//
//  PlantTool.m
//  植物管理
//
//  Created by liujianlong on 2022/11/22.
//

#import "PlantTool.h"

@implementation PlantTool
#pragma mark - 找笔记
+ (void)find:(nullable void(^) (NSMutableArray <Plant*>*result))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@",
                       bg_sqlKey(@"account"),bg_sqlValue(APP.user.account)];
    [self find:where complete:block];
}

#pragma mark - 找文件夹里面的笔记
+ (NSInteger)count:(NSInteger)stamp complete:(nullable void(^) (NSMutableArray <Plant*>*result))block{
    NSString *year = [NSDate transToDate:@"yyyy" :stamp];
    NSString *month = [NSDate transToDate:@"MM" :stamp];
    NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ and %@=%@",
                       bg_sqlKey(@"account"),bg_sqlValue(APP.user.account),
                       bg_sqlKey(@"year"),bg_sqlValue(year),
                       bg_sqlKey(@"month"),bg_sqlValue(month)];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self find:where complete:block];
    });
    return [Plant bg_count:nil where:where];
}
#pragma mark - 找到收藏的笔记，和 文件夹
+ (void)findFavorite:(nullable void(^) (NSMutableArray <Plant*>*result))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ and %@=%@ and %@=%@",
                       bg_sqlKey(@"account"),bg_sqlValue(APP.user.account),
                       bg_sqlKey(@"favorite"),bg_sqlValue(@(YES)),
                       bg_sqlKey(@"isDelete"),bg_sqlValue(@(NO)),
                       bg_sqlKey(@"fileName"),bg_sqlValue(@"")];
    [self find:where complete:block];
    
}
#pragma mark - 找到移到废纸篓的笔记，和 文件夹
+ (void)findDelete:(nullable void(^) (NSMutableArray <Plant*>*result))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ and %@=%@",
                       bg_sqlKey(@"account"),bg_sqlValue(APP.user.account),
                       bg_sqlKey(@"isDelete"),bg_sqlValue(@(YES)),
                       bg_sqlKey(@"fileName"),bg_sqlValue(@"")];
    [self find:where complete:block];
}
#pragma mark - 更新笔记，和文件夹里面的笔记
+ (void)update:(Plant *)model complete:(nullable void(^) (BOOL success))block{
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        [model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
            dispatch_async(dispatch_get_main_queue(), ^{
                dispatch_group_leave(group);
            });
        }];
    });
    
    //    if (Plant.isFile) {
    //        dispatch_group_enter(group);
    //        dispatch_group_async(group, queue, ^{
    //
    //            [self findFilePlant:Plant isDelete:NO complete:^(NSMutableArray<Plant *> * _Nonnull result) {
    //                for (Plant *model in result) {
    //                    model.favorite = Plant.favorite;
    //                    model.isDelete = Plant.isDelete;
    //                }
    //                if (result == nil || result.count <= 0) {
    //                    dispatch_group_leave(group);
    //                }else{
    //                    [Plant bg_saveOrUpdateArrayAsync:result complete:^(BOOL isSuccess) {
    //                        dispatch_group_leave(group);
    //                    }];
    //                }
    //            }];
    //        });
    //    }
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [NotifyTool post:notifyPlantUpdate];
        if (block) {
            block(YES);
        }
    });
}
#pragma mark - 搜索笔记
+ (void)search:(NSString *)text complete:(nullable void(^) (NSMutableArray <Plant*>*result))block{
    NSString *iscontainer = [NSString stringWithFormat:@" %@=%@ ",bg_sqlKey(@"isFile"),bg_sqlValue(@(NO))];
    NSString *name = [NSString stringWithFormat:@" %@ like '%%%@%%' ",bg_sqlKey(@"name"),text];
    NSString *remark = [NSString stringWithFormat:@" %@ like '%%%@%%' ",bg_sqlKey(@"account"),text];
    NSString *container = [NSString stringWithFormat:@" %@ like '%%%@%%' ",bg_sqlKey(@"content"),text];
    NSString* where = [NSString stringWithFormat:@"where%@and%@or%@or%@",iscontainer,name,remark,container];
    [self find:where complete:block];
}

#pragma mark - 删除
+ (void)delete:(Plant*)model complete:(nullable void(^) (BOOL success))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@",
                       bg_sqlKey(@"account"),bg_sqlValue(APP.user.account),
                       bg_sqlKey(@"stamp"),bg_sqlValue(@(model.stamp))];
    [Plant bg_deleteAsync:nil where:where complete:^(BOOL isSuccess) {
        dispatch_sync(dispatch_get_main_queue(), ^{
            [NotifyTool post:notifyPlantUpdate];
            if (block) {
                block(isSuccess);
            }
        });
    }];
}



+ (void)find:(NSString *)where complete:(nullable void(^) (NSMutableArray <Plant*>*result))block{
    [Plant bg_findAsync:nil where:where complete:^(NSArray * _Nullable array) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (block) {
                block([NSMutableArray arrayWithArray:array]);
            }
        });
    }];
}
@end
